package com.yilin.platform.service.impl.truck;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.platform.base_core.application.PageData;
import com.yilin.platform.base_core.commons.entity.AuthStatus;
import com.yilin.platform.base_core.commons.utils.ParamUtil;
import com.yilin.platform.base_core.tools.QueryUtil;
import com.yilin.platform.entity.truck.TransportLicense;
import com.yilin.platform.entity.user.Account;
import com.yilin.platform.mapper.truck.TransportLicenseMapper;
import com.yilin.platform.service.truck.ITransportLicenseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.Date;

/**
 * 道路运输证服务
 *
 * @author zhangjinyuan
 * @date 2022/11/11
 */
@Service
@Transactional
public class TransportLicenseService extends ServiceImpl<TransportLicenseMapper, TransportLicense> implements ITransportLicenseService {
   @Resource
   private  TransportLicenseMapper transportLicenseMapper;

    /**
     * 获取运输证分页
     */
    @Override
    public PageData<TransportLicense> getTransportLicensePage(Account loginUser, TransportLicense query, Integer start, Integer limit) {
        QueryWrapper<TransportLicense> queryWrapper = new QueryWrapper<>();
        QueryUtil.buildOrganizeIdOrSuperiorPlatIdWrapper(queryWrapper,loginUser);
        QueryUtil.andQueryWrapper(queryWrapper,query);
        return page(new PageData<>(start, limit), queryWrapper);
    }
    /**
     * 保存道路运输证
     */
    @Override
    public void saveTransportLicense(Account user, TransportLicense transportLicense) {
        ParamUtil.isNull(transportLicense.getVehicleId(), "车辆ID为空");
        ParamUtil.isNull(transportLicense.getVehicleNo(), "车牌号为空");
        ParamUtil.isNull(transportLicense.getTransLicensePhoto(), "图片为空");
        ParamUtil.isNull(transportLicense.getTransLicenseNo(), "司机运输证编号为空");
        ParamUtil.isNull(transportLicense.getTransLicenseName(), "运输证户名为空");
        transportLicense.setOrganizeId(user.getOrganizeId());
        transportLicense.setOrganizeName(user.getOrganizeName());
        if(user.getSuperiorPlatId()!=null){
            transportLicense.setSuperiorPlatId(user.getSuperiorPlatId());
            transportLicense.setSuperiorPlatName(user.getSuperiorPlatName());
        }
        transportLicense.setAuthStatus(AuthStatus.waitProcess);
        if (transportLicense.getId() == null) {
            transportLicense.setAddUserId(user.getId());
            transportLicense.setCreateTime(new Date());
            save(transportLicense);
        } else {
            transportLicense.setUpdateUserId(user.getId());
            transportLicense.setUpdateTime(new Date());
            updateById(transportLicense);
        }
    }

    /**
     * 审核并更新道路运输证数据
     */
    @Override
    public void verifyAndUpdateAuth(Account loginUser, TransportLicense transportLicense) {
//        TruckVehicle truckVehicleById = vehicleService.getById(transportLicense.getVehicleId());
//        Organize organize = organizeService.getOrganizeById(truckVehicleById.getOrganizeId());
//        Account account = accountService.getAccountById(organize.getManagerUserId());
        transportLicense.setAuthStatus(transportLicense.getAuthStatus());
        transportLicense.setFailedMsg(transportLicense.getFailedMsg());
        transportLicense.setAutTime(new Date());
        transportLicense.setAutUserId(loginUser.getId());
        transportLicense.setAutUserName(loginUser.getName());
        transportLicense.setUpdateTime(new Date());
        transportLicense.setUpdateUserId(loginUser.getId());
//        if (transportLicense.getAuthStatus() != AuthStatus.authSuccess) {
//            String text = String.format("您的道路运输证认证失败，请尽快登录APP重新认证。失败原因:%s", transportLicense.getFailedMsg());
//        } else {
//            TruckVehicle truckVehicle = vehicleService.getById(transportLicense.getVehicleId());
//            truckVehicle.setTmsTruckDataState(TruckVehicle.TmsTruckDataState.creating);
//            vehicleService.updateById(truckVehicle);
//            String TransportLicenseText = "您的道路运输证认证成功了！您现在可以登录APP进行下一步操作~";
//            ParamUtil.isSuccess(messageRemote.sendSystemNotify(account.getId(),TransportLicenseText, null));
////            messageRemote.sendPhoneMessage(account.getPhone(), TransportLicenseText);
//        }
        updateById(transportLicense);
    }

    @Override
    public TransportLicense getTransportLicenseByTruckId(String truckId){
        QueryWrapper<TransportLicense> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("vehicle_id", truckId);
        return transportLicenseMapper.selectOne(queryWrapper);
    }

}
